package com.yy.base.util.word;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;

public class WordUtil {
	/**
	 * word文档内容拷贝
	 * @param tlpId
	 * @param filePath
	 * @param outPath
	 * @return
	 * @throws Exception
	 */
	public void exportWord(String filePath, String outPath, String fileName) throws Exception{
		int pos = 1;
		CustomXWPFDocument allDoc = new CustomXWPFDocument();
		boolean flag = true;
		OPCPackage opc = null;
		try {
			opc = OPCPackage.open(filePath);
			XWPFDocument doc = new XWPFDocument(opc);
			
			List<IBodyElement> bodylist = doc.getBodyElements();
			for (IBodyElement iBodyElement : bodylist) {
				BodyElementType o = iBodyElement.getElementType();
				//word文档表格
				if("TABLE".equals(o.name())){
					if(pos >1 && flag){
						allDoc.createParagraph().createRun().addBreak(BreakType.PAGE);
					}
					XWPFTable table = (XWPFTable)iBodyElement;
					XWPFTable newTable = allDoc.createTable();
					
					newTable.setColBandSize(table.getColBandSize());
					newTable.setRowBandSize(table.getRowBandSize());
					newTable.setWidth(table.getWidth());
					newTable.setInsideHBorder(table.getInsideHBorderType(), 
							table.getInsideHBorderSize(), table.getInsideHBorderSpace(), table.getInsideHBorderColor());
					newTable.setInsideVBorder(table.getInsideVBorderType(), table.getInsideVBorderSize(), 
							table.getInsideVBorderSpace(), table.getInsideVBorderColor());
					newTable.setCellMargins(table.getCellMarginTop(), table.getCellMarginLeft(), 
							table.getCellMarginBottom(), table.getCellMarginRight());
					
					CTTbl old_ttbl = table.getCTTbl();
					if (old_ttbl != null) {
						CTTblPr old_tblPr = old_ttbl.getTblPr();
						if (old_tblPr != null && old_tblPr.isSetTblW()) {
							CTTblWidth old_tblWidth = old_tblPr.getTblW();
							CTJc old_cTJc = old_tblPr.addNewJc();
							
							//设置表格样式
							CTTbl ttbl = newTable.getCTTbl();
							if (ttbl != null) {
								CTTblPr tblPr = ttbl.getTblPr() != null ? ttbl.getTblPr() : ttbl.addNewTblPr();
								if (tblPr.isSetTblW()) {
									CTTblWidth tblWidth = tblPr.getTblW();
									CTJc cTJc = tblPr.addNewJc();
									
									cTJc.setVal(old_cTJc.getVal());
									tblWidth.setW(old_tblWidth.getW());
									tblWidth.setType(old_tblWidth.getType());
								}
							}
						}
					}
					
					
					newTable.removeRow(0);
					for (XWPFTableRow row : table.getRows()) {
						newTable.addRow(row);
					}
					flag = false;
					pos++;
				} else if("PARAGRAPH".equals(o.name())) {//word文档段落
					if(pos > 1 && flag){
						allDoc.createParagraph().createRun().addBreak(BreakType.PAGE);
					}
					
					XWPFParagraph oldParagraph = (XWPFParagraph)iBodyElement;
					XWPFParagraph newParagraph = allDoc.createParagraph();
					//设置段落样式
					newParagraph.setAlignment(oldParagraph.getAlignment());
					
					if (oldParagraph.getBorderBetween() != null) {
						newParagraph.setBorderBetween(oldParagraph.getBorderBetween());
					}
					if (oldParagraph.getBorderBottom() != null) {
						newParagraph.setBorderBottom(oldParagraph.getBorderBottom());
					}
					if (oldParagraph.getBorderLeft() != null) {
						newParagraph.setBorderLeft(oldParagraph.getBorderLeft());
					}
					if (oldParagraph.getBorderLeft() != null) {
						newParagraph.setBorderRight(oldParagraph.getBorderRight());
					}
					if (oldParagraph.getBorderLeft() != null) {
						newParagraph.setBorderTop(oldParagraph.getBorderTop());
					}
					newParagraph.setSpacingBefore(oldParagraph.getSpacingBefore());
					newParagraph.setSpacingAfter(oldParagraph.getSpacingAfter());
					
//						newParagraph.setIndentationFirstLine(oldParagraph.getIndentationFirstLine());
//						newParagraph.setIndentationHanging(oldParagraph.getIndentationHanging());
//						newParagraph.setIndentationLeft(oldParagraph.getIndentationLeft());
//						newParagraph.setIndentationRight(oldParagraph.getIndentationRight());
//						newParagraph.setSpacingAfterLines(oldParagraph.getSpacingAfterLines());
//						newParagraph.setSpacingBeforeLines(oldParagraph.getSpacingBeforeLines());
					
					if (oldParagraph.getSpacingLineRule() != null) {
						newParagraph.setSpacingLineRule(oldParagraph.getSpacingLineRule());
					}
					if (oldParagraph.getStyle() != null) {
						newParagraph.setStyle(oldParagraph.getStyle());
					}
					if (oldParagraph.getVerticalAlignment() != null) {
						newParagraph.setVerticalAlignment(oldParagraph.getVerticalAlignment());
					}
					if (oldParagraph.isWordWrap()) {
						newParagraph.setWordWrap(oldParagraph.isWordWrap());
					}
					
					//设置首行缩进
					if (oldParagraph.getCTP() != null) {
						if (oldParagraph.getCTP().getPPr() != null) {
							CTPPr pPPr = oldParagraph.getCTP().getPPr();
							newParagraph.getCTP().setPPr(pPPr);
						}
					}
					
					for (XWPFRun run : oldParagraph.getRuns()) {
						XWPFRun nRun = newParagraph.createRun();
						nRun.setText(run.getText(run.getTextPosition()));
						
						nRun.setBold(run.isBold());
						nRun.setColor(run.getColor());
						nRun.setFontFamily(run.getFontFamily());
						nRun.setItalic(run.isItalic());
						nRun.setStrike(run.isStrike());
						nRun.setSubscript(run.getSubscript());
						nRun.setUnderline(run.getUnderline());

						if (run.getCTR() != null) {
							CTRPr pRpr = run.getCTR().getRPr();
							if (pRpr != null) {
								nRun.getCTR().setRPr(pRpr);
							}
						}
					}
					pos++;
					flag = false;
				}
			}
		} finally {
			if (opc != null) {
				opc.close();
			}
		}
		File dirPath = new File(outPath);
		if (!dirPath.exists()) {
			dirPath.mkdir();
		}
		saveDocument(allDoc, outPath + fileName);
	}
	
	private void saveDocument(XWPFDocument document, String savePath) throws Exception {
		FileOutputStream fos = new FileOutputStream(savePath);
		document.write(fos);
		fos.close();
	}
}
